<div id="tb<?php echo $uniqid;?>">
    <table cellspacing="0" cellpadding="0">
        <tbody>
        <tr>
            <td><a href="javascript:void(0);" onclick="bitchBepartake();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont"></i><span class="l-btn-text">批量参与</span></span></a></td>
            <td><a href="javascript:void(0);" onclick="bitchNopartake();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont"></i><span class="l-btn-text">批量不参与</span></span></a></td>
            <td><a href="javascript:void(0);" onclick="agentgoodsReload();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shuaxin"></i><span class="l-btn-text">重载</span></span></a></td>
            <td><span style="line-height:30px;"></span>
                <input id="agentgoodskeywords" style="width:120px" placeholder="按ID、商品名搜索" type="text"
                                   class="easyui-input textbox"></td>
            <td><a href="javascript:void(0);" onclick="agentgoodsSearch();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-sousuo"></i><span class="l-btn-text">查询</span></span></a></td>
        </tr>
        </tbody>
    </table>
</div>
<div class="con"  style="-moz-user-select:none;">
    <table id="agentgoods"></table>
</div>
<div id="agentgoodsDlg" class="easyui-dialog" style="width:840px; height:520px;" title="信息框" closed="true"></div>
<script>
    var bonus = function (id) {
        let title = '设置分红';
        let rowId = id || ($('#agentgoods').datagrid('getSelected') && $('#agentgoods').datagrid('getSelected').id);
        if (rowId) {
            let url = '/admin/agent/bonus?id=' + rowId;
            $('#rightTabs').tabs('close', title);
            addTab(title, url);
        } else {
            $.messager.alert('提示', '请先选择要编辑的记录！', 'warning');
        }
        event.stopPropagation();
    };
    var bepartake = function (id) {
        let rowId = id || ($('#agentgoods').datagrid('getSelections') && $('#agentgoods').datagrid('getSelections').length>0);
        if (rowId) {
            $.post('/admin/agent/bepartake', {id:rowId}, function(data){
                if(parseInt(data.ret)===0){
                    $.messager.show({
                        title:'提示',
                        msg:data.msg,
                        timeout:3000,
                        showType:'slide'
                    });
                    agentgoodsReload();
                }else{
                    $.messager.alert('提示',data.msg,'warning');
                }
            },'json');
        } else {
            $.messager.alert('提示', '请先选择要编辑的行！', 'warning');
        }
        event.stopPropagation();
    };
    var bitchBepartake = function () {
        let rowId = $('#agentgoods').datagrid('getSelections');
        if (rowId.length>0) {
            let ids = [];
            rowId.forEach((it)=>{
                ids.push(it.id);
            });
            $.post('/admin/agent/bepartake', {ids:ids}, function(data){
                if(parseInt(data.ret)===0){
                    $.messager.show({
                        title:'提示',
                        msg:data.msg,
                        timeout:3000,
                        showType:'slide'
                    });
                    agentgoodsReload();
                }else{
                    $.messager.alert('提示',data.msg,'warning');
                }
            },'json');
        } else {
            $.messager.alert('提示', '请先选择要编辑的行！', 'warning');
        }
        event.stopPropagation();
    };
    var nopartake = function (id) {
        let rowId = id || ($('#agentgoods').datagrid('getSelections') && $('#agentgoods').datagrid('getSelections').length>0);
        if (rowId) {
            $.post('/admin/agent/nopartake', {id:rowId}, function(data){
                if(parseInt(data.ret)===0){
                    $.messager.show({
                        title:'提示',
                        msg:data.msg,
                        timeout:3000,
                        showType:'slide'
                    });
                    agentgoodsReload();
                }else{
                    $.messager.alert('提示',data.msg,'warning');
                }
            },'json');
        } else {
            $.messager.alert('提示', '请先选择要编辑的行！', 'warning');
        }
        event.stopPropagation();
    };
    var bitchNopartake = function () {
        let rowId = $('#agentgoods').datagrid('getSelections');
        if (rowId.length>0) {
            let ids = [];
            rowId.forEach((it)=>{
                ids.push(it.id);
            });
            $.post('/admin/agent/nopartake', {ids:ids}, function(data){
                if(parseInt(data.ret)===0){
                    $.messager.show({
                        title:'提示',
                        msg:data.msg,
                        timeout:3000,
                        showType:'slide'
                    });
                    agentgoodsReload();
                }else{
                    $.messager.alert('提示',data.msg,'warning');
                }
            },'json');
        } else {
            $.messager.alert('提示', '请先选择要编辑的行！', 'warning');
        }
        event.stopPropagation();
    };
    var agentgoodsReload = function () {
        $("#agentgoods").datagrid('reload');
    };
    var agentgoodsSearch = function () {
        var height = $(window).height() - $(".top").height() - 46;//表格高度
        var width = $(window).width() - $(".leftmenu").width() - 8;
        $("#agentgoods").datagrid({
            title: '代理商品',
            width: width,
            height: height,//高度
            singleSelect: false,//如果为true，则只允许选择一行
            striped: true,//是否显示斑马线效果。
            rownumbers: false,//如果为true，则显示一个行号列。
            pagination: true,//是否显示分页
            pageSize: 15,
            pageList: [15, 30, 40, 50],
            method: 'post',
            sortName: 'weigh',
            sortOrder: 'desc',
            url: '/admin/agent/goods',
            queryParams: {
                keywords: $('#agentgoodskeywords').val(),
            },
            fitColumns: true,//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
            nowrap: false,//如果为true，则在同一行中显示数据。设置为true可以提高加载性能。 是否换行
            selectOnCheck: true,
            checkOnSelect: true,
            idField: 'id',
            toolbar: '#tb<?php echo $uniqid;?>',
            rowStyler: function (index, row) {
                if (row.status == '失效') {
                    return 'color:#F00;font-style:italic;';
                }
            },
            columns: [[
                {field: 'id', checkbox: true, width: fixWidth(5)},
                {
                    field: 'ids',
                    title: 'ID',
                    width: '10%',
                    formatter: function (value, rowData, rowIndex) {
                        return rowData.id;
                    }
                },
                {
                    field: 'title', title: '商品信息', width: fixWidth(50), formatter: function (value, rowData, rowIndex) {
                        return '<div style="display: flex;align-items: center;">' +
                            '<img src="' + rowData.image + '?imageView2/1/w/60/h/60" style="height:30px;width:30px;border-radius: 5px;border:#dfdfdf 1px solid;">'+
                            '<div>' + value + "<br>" + (rowData.is_sku ? '（多规格）' : '') + '</div></div>';
                    }
                },
                {field: 'price', title: '价格', width: fixWidth(15), align:'center'},
                {
                    field: 'option', title: '操作', align:'center', width: fixWidth(15), formatter: function (value, rowData, rowIndex) {
                        let buttons = '<div class="table"><span class="l-btn l-btn-small color9 table" onclick="bonus(\'' + rowData.id + '\')"><span class="l-btn-text">设置分红</span></span>';
                        if (rowData.goods_id>0 && parseInt(rowData.status)===1) {
                            buttons += '<span class="l-btn l-btn-small color9 table" onclick="nopartake(\'' + rowData.id + '\')"><span class="l-btn-text" style="color: red">不参与</span></span>';
                        }else if (rowData.goods_id>0 && parseInt(rowData.status)===0) {
                            buttons += '<span class="l-btn l-btn-small color9 table" onclick="bonus(\'' + rowData.id + '\')"><span class="l-btn-text">参与</span></span>';
                        }else{
                            buttons += '<span class="l-btn l-btn-small color9 table" onclick="bepartake(\'' + rowData.id + '\')"><span class="l-btn-text">参与</span></span>';
                        }
                        return buttons + '</div>';
                    }
                },
            ]],
        });
    };
    agentgoodsSearch();
    onDatagridResize("#agentgoods");
</script>
